﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.JinShengService;
using System.Collections.ObjectModel;
using CommunitySystem.PublicOfficialSelectionService;
using CommunitySystem.EmpbasicService;
using System.IO;
using EHR.Common;

namespace CommunitySystem.PageView
{
    public partial class EditJinshengWindow : ChildWindow
    {
        private ObservableCollection<JinShen_MainInfo> entitys = new ObservableCollection<JinShen_MainInfo>();
        private PublicOfficialSelectionServiceClient _uploadClient = new PublicOfficialSelectionServiceClient();
        private JinShengServiceClient _client = new JinShengServiceClient();

        private JinShen_MainInfo_Attatchment jinshenatt= null;

        private JinShenMHelp jinshengHelp = null;
        private string isqx = "";
        public EditJinshengWindow(JinShenMHelp item, string button)
        {
            InitializeComponent();
            //没有方案则不显示相关方案字样
            if (string.IsNullOrEmpty(item.realname) && button != JinShenConst.ACTION_Solution_Modify) spjsfa.Visibility = Visibility.Collapsed;

            isqx = button;
            jinshengHelp = item;
            if (item != null)
                this.dtorgid.DataContext = item;
          //  this.dtbOrg.SelectValue = item.OrgName;

        

            this.tbDanwei.Text = item.OrgName.ToString();
            entitys = item.JinShen_MainInfos;
            
            //保存审核相关记录
            _client.AddJinShen_SHJLDetailCompleted += (ss, ee) =>
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功");
                this.DialogResult = true;
            };
            //消息
            _client.AddSystemInfoCompleted += (ss, ee) =>
            {

            };
            ///保存
            _client.SaveJinShenMainInfosCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                  
                    // SHJL();
                    SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功");
                    this.DialogResult = true;

                    //添加消息
                    SystemInfo sysinfo = new SystemInfo();
                    sysinfo.MenuId = 376;
                    sysinfo.MoKuai = "晋升管理";
                    if (item.Status.Equals("提交申请")) 
                    {
                        sysinfo.MoKuaiSX = "增加人员";
                        sysinfo.CreateUserID = 2;
                    }
                    if (item.Status.Equals("审核通过"))
                    {
                        sysinfo.MoKuaiSX = "人员审核";
                        sysinfo.CreateUserID = 1;
                    }
                    if (item.Status.Equals("人员审核"))
                    {
                        sysinfo.MoKuaiSX = "民主测评及竞争上岗";
                        sysinfo.CreateUserID = 2;
                    }
                    if (item.Status.Equals("人员审核通过"))
                    {
                        sysinfo.MoKuaiSX = "竞争上岗及民主测评情况查看";
                        sysinfo.CreateUserID = 1;
                    }
                    if (item.Status.Equals("已查看"))
                    {
                        sysinfo.MoKuaiSX = "人员考察";
                        sysinfo.CreateUserID = 2;
                    }
                   
                    //if (button == JinShenConst.ACTION_Candidate_New)  //增加人员
                    //{
                    //    sysinfo.MoKuaiSX = "增加人员";
                    //    sysinfo.CreateUserID = 2;
                    //}
                    //if (button == JinShenConst.ACTION_Candidate_Check) //人员审核
                    //{
                    //    sysinfo.MoKuaiSX = "人员审核";
                    //    sysinfo.CreateUserID = 1;
                    //} 
                    //if (button == JinShenConst.ACTION_Candidate_Evalution) //民主测评
                    //{
                    //    sysinfo.MoKuaiSX = "民主测评及竞争上岗";
                    //    sysinfo.CreateUserID = 2;
                    //} if (button == JinShenConst.ACTION_Candidate_Approval) //人员核定
                    //{
                    //    sysinfo.MoKuaiSX = "人员核定";
                    //    sysinfo.CreateUserID = 2;
                    //}
                        sysinfo.DW1 = item.OrgName;
                    sysinfo.CreateUser = Session.LoginUser.RealName;
                    sysinfo.CreateDate = DateTime.Now.ToString();
                  
                    sysinfo.OrgIDs = item.OrgID.ToString();
                    sysinfo.LogUID = Session.LoginUser.UserID;
                    _client.AddSystemInfoAsync(sysinfo);
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("保存失败，可能原因:\n" + e.Error.Message);
                }
            };

            if (button==JinShenConst.ACTION_Solution_Modify)
            {
                this.Title = "编辑晋升信息";


                InitDep(JinShenConst.ACTION_Solution_Modify);

    
                this.LayoutRoot.RowDefinitions[2].Height = new GridLength(50);
                wpJinsheng.Visibility = Visibility.Collapsed;
                Page(null);

            }

            if (button== JinShenConst.ACTION_Solution_Check)
            {
                this.Title = "晋升方案审核";

                InitDep(JinShenConst.ACTION_Solution_Check);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed;
                btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }

            if (button == JinShenConst.ACTION_Candidate_New)
            {
                this.Title = "增加候选人";

                InitDep(JinShenConst.ACTION_Candidate_New);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
            if (button == JinShenConst.ACTION_Candidate_Check)
            {
                this.Title = "人员审核";

                InitDep(JinShenConst.ACTION_Candidate_Check);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
            if (button == JinShenConst.ACTION_Candidate_Evalution)
            {
                this.Title = "民主测评及竞争上岗";

                InitDep(JinShenConst.ACTION_Candidate_Evalution);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
            if (button == JinShenConst.ACTION_Candidate_Approval)
            {
                this.Title = "人员核定";

                InitDep(JinShenConst.ACTION_Candidate_Approval);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
        }

        private bool _isck = false;
        public EditJinshengWindow(JinShenMHelp item, string button,bool isck)
        {
            InitializeComponent();
            _isck = isck;
            this.OKButton.Visibility = Visibility.Collapsed;
            isqx = button;
            jinshengHelp = item;
            if (item != null)
                this.dtorgid.DataContext = item;
            //  this.dtbOrg.SelectValue = item.OrgName;



            this.tbDanwei.Text = item.OrgName.ToString();
            entitys = item.JinShen_MainInfos;

            //保存审核相关记录
            _client.AddJinShen_SHJLDetailCompleted += (ss, ee) =>
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功");
                this.DialogResult = true;
            };

            ///保存
            _client.SaveJinShenMainInfosCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {

                    #region
                   // SHJL();
                    SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功");
                    this.DialogResult = true;
                    #endregion
                
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("保存失败，可能原因:\n" + e.Error.Message);
                }
            };

            if (button == JinShenConst.ACTION_Solution_Modify)
            {
                this.Title = "编辑晋升信息";


                InitDep(JinShenConst.ACTION_Solution_Modify);

                //StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                //sp.Children.Add(new TextBlock() { Text = item.realname, });
                //Image img = new Image() { Cursor = Cursors.Hand, Height = 15, Width = 15, Margin = new Thickness(5, 0, 0, 0) };
                //img.Source = new System.Windows.Media.Imaging.BitmapImage(
                //    new Uri("/CommunitySystem;component/Images/ViewDesign/delete.png",
                //        UriKind.RelativeOrAbsolute));
                //sp.Children.Add(img);
                //img.MouseLeftButtonDown += (sender1, e1) =>
                //{
                //    wpJinsheng.Children.Remove(sp);
                //    // files.Remove(item.filename);
                //};
                //wpJinsheng.Children.Add(sp);
                this.LayoutRoot.RowDefinitions[2].Height = new GridLength(50);
                wpJinsheng.Visibility = Visibility.Collapsed;
                Page(null);

            }

            if (button == JinShenConst.ACTION_Solution_Check)
            {
                this.Title = "晋升方案审核";

                InitDep(JinShenConst.ACTION_Solution_Check);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed;
                btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }

            if (button == JinShenConst.ACTION_Candidate_New)
            {
                this.Title = "增加候选人";

                InitDep(JinShenConst.ACTION_Candidate_New);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
            if (button == JinShenConst.ACTION_Candidate_Check)
            {
                this.Title = "人员审核";

                InitDep(JinShenConst.ACTION_Candidate_Check);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
            if (button == JinShenConst.ACTION_Candidate_Evalution)
            {
                this.Title = "民主测评及竞争上岗";

                InitDep(JinShenConst.ACTION_Candidate_Evalution);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
            if (button == JinShenConst.ACTION_Candidate_Approval)
            {
                this.Title = "人员核定";

                InitDep(JinShenConst.ACTION_Candidate_Approval);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
        }

        private void SHJL()
        {
            JinShen_SHJLDetail shjl = new JinShen_SHJLDetail();
            if (entitysC != null && entitysC.Count > 0)
            {
                var jsentity = entitysC.First();
                if (jsentity.Status != JinShenConst.STATUS_Solution_Submit)
                {
                    shjl.JSMID = entitysC.First().MId;

                    //审核通过  审核不通过  
                    if (jsentity.Status == JinShenConst.STATUS_SolutionPass || jsentity.Status == JinShenConst.STATUS_Solution_Retire)
                    {
                        shjl.Name = "方案审核";
                        shjl.Status = jsentity.Status;
                        shjl.BLDate = shjl.CreateDate = DateTime.Now;
                        shjl.CreateUser = Session.LoginUser.LoginName;
                    }

                    if (jsentity.Status == JinShenConst.STATUS_CandidateChecking)
                    {
                        shjl.Name = "人员审核";
                    }
                    //人员审核通过  审核不通过  
                    if (jsentity.Status == JinShenConst.STATUS_CandidateCheck_Pass || jsentity.Status == JinShenConst.STATUS_CandidateCheck_UnPass)
                    {
                        shjl.Name = "人员审核";
                        shjl.Status = jsentity.Status;
                        shjl.BLDate = shjl.CreateDate = DateTime.Now;
                        shjl.CreateUser = Session.LoginUser.LoginName;
                    }

                    if (jsentity.Status == JinShenConst.STATUS_Candidate_Look)
                    {
                        shjl.Name = "竞争上岗及民主测评情况";
                    }
                    //部门领导查看
                    if (jsentity.Status == JinShenConst.STATUS_Candidate_LookTrue)
                    {
                        shjl.Name = "竞争上岗及民主测评情况";
                        shjl.Status = jsentity.Status;
                        shjl.BLDate = shjl.CreateDate = DateTime.Now;
                        shjl.CreateUser = Session.LoginUser.LoginName;
                    }

                  
                    //人员核定 
                    if (jsentity.Status == JinShenConst.STATUS_Candidate_Inspect)
                    {
                        shjl.Name = "人员核定情况";
                        shjl.Status = jsentity.Status;
                        shjl.BLDate = shjl.CreateDate = DateTime.Now;
                        shjl.CreateUser = Session.LoginUser.LoginName;
                    }



                    if (!string.IsNullOrEmpty(shjl.Name))
                        _client.AddJinShen_SHJLDetailAsync(shjl, entitysC.First());
                }
            }
        }


        public EditJinshengWindow(JinShenMHelp item, string button,string bm)
        {
            InitializeComponent();
            isqx = button;
            jinshengHelp = item;
            if (item != null)
                this.dtorgid.DataContext = item;
            //  this.dtbOrg.SelectValue = item.OrgName;



            this.tbDanwei.Text = item.OrgName.ToString();
            entitys = item.JinShen_MainInfos;

            //保存审核相关记录
            _client.AddJinShen_SHJLDetailCompleted += (ss, ee) => { };
            //消息
            _client.AddSystemInfoCompleted += (ss, ee) =>
            {

            };
            ///保存
            _client.SaveJinShenMainInfosCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功");
                    #region
                    //JinShen_SHJLDetail shjl = new JinShen_SHJLDetail();
                    //if (entitysC != null && entitysC.Count > 0)
                    //{
                    //    var jsentity = entitysC.First();
                    //    if (jsentity.Status != JinShenConst.STATUS_Solution_Submit)
                    //    {
                    //        shjl.JSMID = entitysC.First().MId;
                    //        //审核通过  审核不通过  
                    //        if (jsentity.Status == JinShenConst.STATUS_SolutionPass || jsentity.Status == JinShenConst.STATUS_Solution_Retire)
                    //            shjl.Name = "方案审核";

                    //        //人员审核通过  审核不通过  
                    //        if (jsentity.Status == JinShenConst.STATUS_CandidateCheck_Pass || jsentity.Status == JinShenConst.STATUS_CandidateCheck_UnPass)
                    //            shjl.Name = "人员审核";

                    //        //部门领导查看
                    //        if (jsentity.Status == JinShenConst.STATUS_Candidate_LookTrue)
                    //            shjl.Name = "竞争上岗及民主测评情况";

                    //        //人员核定 
                    //        if (jsentity.Status == JinShenConst.STATUS_Candidate_Approved)
                    //            shjl.Name = "人员核定情况";

                    //        shjl.Status = jsentity.Status;
                    //        shjl.BLDate = shjl.CreateDate = DateTime.Now;
                    //        shjl.CreateUser = Session.LoginUser.LoginName;

                    //        if (!string.IsNullOrEmpty(shjl.Name))
                    //            _client.AddJinShen_SHJLDetailAsync(shjl, entitysC.First());
                    //    }
                    //}

                    #endregion
                    this.DialogResult = true;

                    //添加消息
                    SystemInfo sysinfo = new SystemInfo();
                    sysinfo.MenuId = 376;
                    sysinfo.MoKuai = "晋升管理";
                    if (item.Status.Equals("主管部门查看"))
                    {
                        sysinfo.MoKuaiSX = "人员核定";
                        sysinfo.CreateUserID = 2;
                    }
                    sysinfo.DW1 = item.OrgName;
                    sysinfo.CreateUser = Session.LoginUser.RealName;
                    sysinfo.CreateDate = DateTime.Now.ToString();

                    sysinfo.OrgIDs = item.OrgID.ToString();
                    sysinfo.LogUID = Session.LoginUser.UserID;
                    _client.AddSystemInfoAsync(sysinfo);

                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("保存失败，可能原因:\n" + e.Error.Message);
                }
            };

            if (button == JinShenConst.ACTION_Solution_Modify)
            {
                this.Title = "编辑晋升信息";


                InitDep(JinShenConst.ACTION_Solution_Modify);

                //StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                //sp.Children.Add(new TextBlock() { Text = item.realname, });
                //Image img = new Image() { Cursor = Cursors.Hand, Height = 15, Width = 15, Margin = new Thickness(5, 0, 0, 0) };
                //img.Source = new System.Windows.Media.Imaging.BitmapImage(
                //    new Uri("/CommunitySystem;component/Images/ViewDesign/delete.png",
                //        UriKind.RelativeOrAbsolute));
                //sp.Children.Add(img);
                //img.MouseLeftButtonDown += (sender1, e1) =>
                //{
                //    wpJinsheng.Children.Remove(sp);
                //    // files.Remove(item.filename);
                //};
                //wpJinsheng.Children.Add(sp);
                this.LayoutRoot.RowDefinitions[2].Height = new GridLength(50);
                wpJinsheng.Visibility = Visibility.Collapsed;
                Page(null);
            }

            if (button == JinShenConst.ACTION_Solution_Check)
            {
                this.Title = "晋升方案审核";

                InitDep(JinShenConst.ACTION_Solution_Check);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }

            if (button == JinShenConst.ACTION_Candidate_New)
            {
                this.Title = "增加候选人";

                InitDep(JinShenConst.ACTION_Candidate_New);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
            if (button == JinShenConst.ACTION_Candidate_Check)
            {
                this.Title = "人员审核";

                InitDep(JinShenConst.ACTION_Candidate_Check);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
            if (button == JinShenConst.ACTION_Candidate_Evalution)
            {
                this.Title = "民主测评及竞争上岗";

                InitDep(JinShenConst.ACTION_Candidate_Evalution,bm);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
            if (button == JinShenConst.ACTION_Candidate_Approval)
            {
                this.Title = "人员核定";

                InitDep(JinShenConst.ACTION_Candidate_Approval);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
        }

        private EmpbasicService.EmpBasicServiceClient Client2 = new EmpbasicService.EmpBasicServiceClient();
        public EditJinshengWindow(JinShenMHelp item, string button,List<string> orgIDs)
        {
            InitializeComponent();

            isqx = button;
            jinshengHelp = item;
            if (item != null)
                this.dtorgid.DataContext = item;
            //  this.dtbOrg.SelectValue = item.OrgName;

            #region
            if (orgIDs[0] == "0")
            {
                tbtiaojian.Visibility = System.Windows.Visibility.Collapsed;
                dtorgid.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                tbtiaojian.Visibility = System.Windows.Visibility.Visible;
                dtorgid.Visibility = System.Windows.Visibility.Collapsed;

                string ord = "";
                foreach (string i in orgIDs)
                {
                    ord = ord + i + ",";
                }
                Client2.GetOrgForHRModelbyIDsCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs>(Client2_GetOrgForHRModelbyIDsCompleted);
                Client2.GetOrgForHRModelbyIDsAsync("(" + ord.Substring(0, ord.Length - 1) + ")");
            }
            #endregion

            this.tbDanwei.Text = item.OrgName.ToString();
            entitys = item.JinShen_MainInfos;

            //保存审核相关记录
            _client.AddJinShen_SHJLDetailCompleted += (ss, ee) => { };
            ///保存
            _client.SaveJinShenMainInfosCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功");
                    #region
                    //JinShen_SHJLDetail shjl = new JinShen_SHJLDetail();
                    //if (entitysC != null && entitysC.Count > 0)
                    //{
                    //    var jsentity = entitysC.First();
                    //    if (jsentity.Status != JinShenConst.STATUS_Solution_Submit)
                    //    {
                    //        shjl.JSMID = entitysC.First().MId;
                    //        //审核通过  审核不通过  
                    //        if (jsentity.Status == JinShenConst.STATUS_SolutionPass || jsentity.Status == JinShenConst.STATUS_Solution_Retire)
                    //            shjl.Name = "方案审核";

                    //        //人员审核通过  审核不通过  
                    //        if (jsentity.Status == JinShenConst.STATUS_CandidateCheck_Pass || jsentity.Status == JinShenConst.STATUS_CandidateCheck_UnPass)
                    //            shjl.Name = "人员审核";

                    //        //部门领导查看
                    //        if (jsentity.Status == JinShenConst.STATUS_Candidate_LookTrue)
                    //            shjl.Name = "竞争上岗及民主测评情况";

                    //        //人员核定 
                    //        if (jsentity.Status == JinShenConst.STATUS_Candidate_Approved)
                    //            shjl.Name = "人员核定情况";

                    //        shjl.Status = jsentity.Status;
                    //        shjl.BLDate = shjl.CreateDate = DateTime.Now;
                    //        shjl.CreateUser = Session.LoginUser.LoginName;

                    //        if (!string.IsNullOrEmpty(shjl.Name))
                    //            _client.AddJinShen_SHJLDetailAsync(shjl, entitysC.First());
                    //    }
                    //}

                    #endregion
                    this.DialogResult = true;
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("保存失败，可能原因:\n" + e.Error.Message);
                }
            };

            if (button == JinShenConst.ACTION_Solution_Modify)
            {
                this.Title = "编辑晋升信息";


                InitDep(JinShenConst.ACTION_Solution_Modify);

                //StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                //sp.Children.Add(new TextBlock() { Text = item.realname, });
                //Image img = new Image() { Cursor = Cursors.Hand, Height = 15, Width = 15, Margin = new Thickness(5, 0, 0, 0) };
                //img.Source = new System.Windows.Media.Imaging.BitmapImage(
                //    new Uri("/CommunitySystem;component/Images/ViewDesign/delete.png",
                //        UriKind.RelativeOrAbsolute));
                //sp.Children.Add(img);
                //img.MouseLeftButtonDown += (sender1, e1) =>
                //{
                //    wpJinsheng.Children.Remove(sp);
                //    // files.Remove(item.filename);
                //};
                //wpJinsheng.Children.Add(sp);
                this.LayoutRoot.RowDefinitions[2].Height = new GridLength(50);
                wpJinsheng.Visibility = Visibility.Collapsed;
                Page(null);

            }

            if (button == JinShenConst.ACTION_Solution_Check)
            {
                this.Title = "晋升方案审核";

                InitDep(JinShenConst.ACTION_Solution_Check);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }

            if (button == JinShenConst.ACTION_Candidate_New)
            {
                this.Title = "增加候选人";

                InitDep(JinShenConst.ACTION_Candidate_New);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
            if (button == JinShenConst.ACTION_Candidate_Check)
            {
                this.Title = "人员审核";

                InitDep(JinShenConst.ACTION_Candidate_Check);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
            if (button == JinShenConst.ACTION_Candidate_Evalution)
            {
                this.Title = "民主测评及竞争上岗";

                InitDep(JinShenConst.ACTION_Candidate_Evalution);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
            if (button == JinShenConst.ACTION_Candidate_Approval)
            {
                this.Title = "人员核定";

                InitDep(JinShenConst.ACTION_Candidate_Approval);

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = item.realname, });
                wpJinsheng.Children.Add(sp);
                hlb.Content = "方案查看列表";
                hlb.IsEnabled = false;
                svjins.Visibility = Visibility.Collapsed; btnSC.Visibility = Visibility.Collapsed;
                this.LayoutRoot.RowDefinitions[1].Height = new GridLength(0);
            }
        }

      
        void Client2_GetOrgForHRModelbyIDsCompleted(object sender, EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs e)
        {
            tbtiaojian.ItemsSource = e.Result;
            for (int i = 0; i < tbtiaojian.Items.Count; i++)
            {
                if (((OrgForHRModel)(tbtiaojian.Items[i])).ID
                    == jinshengHelp.OrgID)
                {
                    tbtiaojian.SelectedIndex = i;
                }
            }
        }
        private JinShen_MainInfo entity = new JinShen_MainInfo();
        private EditJinshengControl editc = null;
        private void InitDep(string qx)
        {
            foreach (var item in entitys)
            {
                entity = item;

                editc = new EditJinshengControl(entity, qx, jinshengHelp.OrgID.Value, _isck, jinshengHelp.Status);
                this.detailsInfo.Children.Add(editc);
            }
        }

        private void InitDep(string qx,string bm)
        {
            foreach (var item in entitys)
            {
                entity = item;

                editc = new EditJinshengControl(entity, qx, bm, jinshengHelp.OrgID.Value, _isck, jinshengHelp.Status);
                this.detailsInfo.Children.Add(editc);
            }
        }

        private readonly string fileDictionary = "file";
        private Dictionary<string, UploadFileInfo> files = new Dictionary<string, UploadFileInfo>();
        /// <summary>
        /// 方案上报
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region
        //private void hlb_Click(object sender, RoutedEventArgs e)
        //{

        //    if (isqx == JinShenConst.ACTION_Solution_Modify)
        //    {
        //        files.Clear();
        //        wpJinsheng.Children.Clear();

        //        if (entity.Attachment.Count > 0)
        //            entity.Attachment.Clear();


        //        var openFile = new OpenFileDialog() { Multiselect = false };
        //        if (openFile.ShowDialog() == true)
        //        {
        //            string GuidName = Guid.NewGuid().ToString("N");
        //            // files.Add(GuidName, openFile.File.OpenRead());
        //            UploadFileInfo uploadFile = new UploadFileInfo() { DisplayName = GuidName, Name = GuidName };
        //            var stream = openFile.File.OpenRead();
        //            uploadFile.Size = stream.Length / 16000;
        //            uploadFile.Context = new List<byte[]>();
        //            int b = 0;

        //            while (stream.Position > -1 && stream.Position < stream.Length)
        //            {
        //                if (stream.Length - stream.Position >= 16000)
        //                {
        //                    b = 16000;
        //                }
        //                else
        //                {
        //                    b = (int)(stream.Length - stream.Position);
        //                }

        //                byte[] filebyte = new byte[b];
        //                stream.Read(filebyte, 0, b);
        //                uploadFile.Context.Add(filebyte);
        //            }
        //            stream.Close();
        //            files.Add(GuidName, uploadFile);
        //            StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
        //            sp.Children.Add(new TextBlock() { Text = openFile.File.Name, });
        //            Image img = new Image() { Cursor = Cursors.Hand, Height = 15, Width = 15, Margin = new Thickness(5, 0, 0, 0) };
        //            img.Source = new System.Windows.Media.Imaging.BitmapImage(
        //                new Uri("/CommunitySystem;component/Images/ViewDesign/delete.png",
        //                    UriKind.RelativeOrAbsolute));
        //            sp.Children.Add(img);
        //            if (entity.Attachment == null)
        //                entity.Attachment = new System.Collections.ObjectModel.ObservableCollection<JinShengService.JinShen_MainInfo_Attatchment>();

        //            var att = new JinShengService.JinShen_MainInfo_Attatchment() { realname = openFile.File.Name, filename = GuidName, Key = fileDictionary };
        //            entity.Attachment.Add(att);
        //            jinshenatt = entity.Attachment.First();
        //            img.MouseLeftButtonDown += (sender1, e1) =>
        //            {
        //                wpJinsheng.Children.Remove(sp);
        //                files.Remove(GuidName);
        //                entity.Attachment.Remove(att);
        //                jinshenatt = null;
        //            };


        //            wpJinsheng.Children.Add(sp);

        //        }
        //    }
        //}
        #endregion
        private ObservableCollection<JinShen_MainInfo> entitysC = null;
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            #region
            //if (files.Count > 0)
            //{
            //    #region 上传文件
            //    _uploadClient.UploadFileCompleted -= new EventHandler<PublicOfficialSelectionService.UploadFileCompletedEventArgs>(_uploadClient_UploadFileCompleted);
            //    _uploadClient.UploadFileCompleted += new EventHandler<PublicOfficialSelectionService.UploadFileCompletedEventArgs>(_uploadClient_UploadFileCompleted);


            //    foreach (var key in files.Keys)
            //    {
            //        var file = files[key];
            //        _uploadClient.UploadFileAsync(fileDictionary, file.DisplayName, file.Context[0], true, file);
            //        file.Context.RemoveAt(0);

            //    }
            //    #endregion
            //}

            //if (jinshenatt != null) jinshenatt.MainID = jinshengHelp.ID;
            #endregion

            if (_files != null && _files.Count != 0)
            {
                if (_files.Where(p => p.State == Constants.FileStates.Pending).Count() > 0)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("您所选的附件还未上传，请点击上传按钮进行上传！");
                    return;
                }
                if (_files.Where(p => p.State == Constants.FileStates.Uploading).Count() > 0)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("附件正在上传，请稍后！");
                    return;
                }
                else
                {
                    if (entity.Attachment == null)
                        entity.Attachment = new System.Collections.ObjectModel.ObservableCollection<JinShen_MainInfo_Attatchment>();
                    else
                        entity.Attachment.Clear();
                    foreach (var item in _files.Where(p => p.State == Constants.FileStates.Finished).ToList())
                    {

                        var att = new JinShen_MainInfo_Attatchment() { realname = item.FileNameG, filename = item.FileName, Key = fileDictionary, Status = (item.State).ToString(), FileSize = item.FileSize.ToString(), Percentage = item.Percentage.ToString() };
                        entity.Attachment.Add(att);
                    }
                    if (entity.Attachment.Count != 0)
                        jinshenatt = entity.Attachment.First();
                }
            }
            if (jinshenatt != null) jinshenatt.MainID = jinshengHelp.ID;
            entitysC = new ObservableCollection<JinShen_MainInfo>();
            foreach (var item in entitys)
            {
                item.OrgID = orgid;
                item.OrgName = orgname;
                entitysC.Add(item); 
            }

            var en = entitysC.Where(p => p.PromotType == "竞争上岗").ToList();
            if (en.Count > 0 && _files!= null)
            {
                if (_files.Count == 0)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("竞争上岗必须上传方案附件!");
                    return;
                }
            }

            //if (isqx != JinShenConst.ACTION_Solution_Modify || isqx != JinShenConst.ACTION_Candidate_New)
                _client.SaveJinShenMainInfosAsync(entitysC, 1, jinshenatt);
            //else
            //{
            //    SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功");

            //    this.DialogResult = true;
            //}

        }
        void _uploadClient_UploadFileCompleted(object sender, PublicOfficialSelectionService.UploadFileCompletedEventArgs e)
        {
            var file = e.UserState as UploadFileInfo;
            if (file.Context.Count > 0)
            {
                _uploadClient.UploadFileAsync(fileDictionary, file.DisplayName, file.Context[0], false, file);
                file.Context.RemoveAt(0);
            }

        }
      

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        public int orgid = -1;
        public string orgname = "";
        private void dtbOrg_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {

            if (e.PropertyName.Equals("SelectItem") && dtorgid.SelectItem != null)
            {
                entity.OrgID = dtorgid.SelectItem.Id;
                orgname = dtorgid.SelectItem.DictCode;
                orgid = dtorgid.SelectItem.Id;

            }

        }

        private void tbtiaojian_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.tbtiaojian.SelectedItem != null)
            {
                entity.OrgID = (tbtiaojian.SelectedItem as OrgForHRModel).ID;
                orgname = (tbtiaojian.SelectedItem as OrgForHRModel).Name;
                orgid = Convert.ToInt32((tbtiaojian.SelectedItem as OrgForHRModel).ID);
            }
        }

        #region  上传附件
        private int _maxFileSize = int.MaxValue;

        private FileCollection _files;
        private int _maxUpload = 4;
        private string _customParams;
        private string _fileFilter;

        public void Page(IDictionary<string, string> initParams)
        {
            _files = new FileCollection(_customParams, _maxUpload);
            if (jinshengHelp != null)
            {
                if (!string.IsNullOrEmpty(jinshengHelp.filename))
                {
                    var uf = new UserFile() { FileName = jinshengHelp.filename, FileNameG = jinshengHelp.realname, State = Constants.FileStates.Finished, Percentage = Convert.ToInt32(jinshengHelp.Percentage), BytesUploaded = Convert.ToDouble(jinshengHelp.FileSize), FileSize = Convert.ToDouble(jinshengHelp.FileSize) };

                    _files.Add(uf);
                }

            }

            FileList.ItemsSource = _files;

        }
        /// <summary>
        /// 方案上报
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hlb_Click(object sender, RoutedEventArgs e)
        {

            #region
            if (_files != null)
                _files.Clear();

            OpenFileDialog ofd = new OpenFileDialog() { Multiselect = false };

            try
            {
                if (!string.IsNullOrEmpty(_fileFilter))
                    ofd.Filter = _fileFilter;
            }
            catch (ArgumentException ex)
            {
                //User supplied a wrong configuration file
                throw new Exception("Wrong file filter configuration.", ex);
            }

            if (ofd.ShowDialog() == true)
            {

                foreach (FileInfo file in ofd.Files)
                {
                    string GuidName = Guid.NewGuid().ToString("N");
                    string fileName = file.Name;

                    UserFile userFile = new UserFile();
                    userFile.FileName = GuidName; //GuidName file.Name;
                    userFile.FileNameG = fileName;
                    userFile.IsAdd = true;
                    userFile.FileStream = file.OpenRead();


                    #region  页面显示
                    foreach (var item in _files)
                    {
                        if (item.FileNameG.Contains(fileName))
                        {
                            SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("此文件已上传,需要覆盖原有文件吗?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
                            cm.Show();
                            cm.OKButton.Click += (sender1, e1) =>
                            {
                                _files.Remove(item);
                            };
                        }
                    }
                    #endregion

                    if (userFile.FileStream.Length <= _maxFileSize)
                    {
                        //向文件列表中添加文件信息
                        if (_files == null) _files = new FileCollection(_customParams, _maxUpload);
                        _files.Add(userFile);
                    }
                    else
                        SilverlightMessageBoxLibrary.Message.InfoMessage("上传最大文件大小：" + (_maxFileSize / 1024).ToString() + "KB.");
                }
            }
            #endregion
        }

        /// <summary>
        /// 上传附件开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ibtnFJ_Click(object sender, RoutedEventArgs e)
        {
            if (_files.Count == 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择上传的附件！");
                return;
            }
            else
            {

                _files.UploadFiles();
            }
        }
        #endregion
    }
}

